package day2_linkedlist.leetcode;

/**
 * @Author: yuisama
 * @Date: 2021/11/2 10:26
 * @Description:删除链表中倒数第K个节点
 */
public class RemoveKNodes {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode fir = dummyHead,sec = dummyHead;
        for (int i = 0; i < n + 1; i++) {
            sec = sec.next;
        }
        while (sec != null) {
            fir = fir.next;
            sec = sec.next;
        }
        fir.next = fir.next.next;
        return dummyHead.next;
    }
}
